﻿#include "FITKAbaqusAdaptorConnectorElasticityBehavior.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "InpReadInformationMapper.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractAssInstance.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConnectorSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConnectorBehaviorElasticity.h"
#include <QDebug>
//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorConnectorElasticityBehavior::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorConnectorElasticityBehavior";
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::adaptR()
    {
        _sectionConnectorData = dynamic_cast<Interface::FITKAbaConnectorSection*>(_dataObj);
        if (!_sectionConnectorData || !_reader)return false;
        //回退一行，处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*connector elasticity")) return false;
        //添加行为
        _elasticityBehaviorData = dynamic_cast<Interface::FITKAbaConnectorBehaviorElasticity*>
            (_sectionConnectorData->addBehavior(Interface::FITKAbaConnectorBehaviorEnum::ConnectorBehaviorType::Elasticity));
        if (!_elasticityBehaviorData)return false;
        //关键字处理
        bool readOK = false;
        InpKeyLineSpliter s(line, _reader);
        //力矩类型
        if (s.hasKey("component"))
        {
            this->_component = s["component"].trimmed().toInt(&readOK);
            if (!readOK)return false;
            if (_component < 1 || _component > 6)return false;
            std::array<bool, 6> forceMoment{ false, false, false, false, false, false };
            forceMoment[_component - 1] = true;
            _elasticityBehaviorData->setForceMomentStates(forceMoment);
        }
        else
        {
            std::array<bool, 6> forceMoment{ true, true, true, true, true, true };
            _elasticityBehaviorData->setForceMomentStates(forceMoment);
        }
        //场变量个数
        if (s.hasKey("dependencies"))
        {
            this->_dependencies = s["dependencies"].trimmed().toInt(&readOK);
            if (!readOK)return false;
        }
        //设置定义类型-非线性
        if (s.hasKey("nonlinear"))
        {
            _elasticityBehaviorData->setDefinition(Interface::FITKAbaConnectorBehaviorEnum::Definition::Nonlinear);
            if (s["independent components"].toLower() == "position")
                readOK = this->readDefinitionNonlinearTypeCoupledPosition();
            else if (s["independent components"].toLower() == "constitutive motion")
                readOK = this->readDefinitionNonlinearTypeCoupledMotion();
            else
                readOK = this->readDefinitionNonlinearTypeUncoupled();
        }
        //设置定义类型-刚性
        else if (s.hasKey("rigid"))
        {
            _elasticityBehaviorData->setDefinition(Interface::FITKAbaConnectorBehaviorEnum::Definition::Rigid);
            readOK = this->readDefinitionRigidType();
        }
        //设置定义类型-线性(默认)
        else
        {
            _elasticityBehaviorData->setDefinition(Interface::FITKAbaConnectorBehaviorEnum::Definition::Linear);
            if (s.hasKey("component"))
                readOK = this->readDefinitionLinearTypeUncoupled();
            else
            {
                //设置不跳过空行
                _reader->setSkipBlankLine(false);
                readOK = this->readDefinitionLinearTypeCoupled();
                //恢复跳过空行
                _reader->setSkipBlankLine(true);
            }
                
        }
        //读取失败移除添加的行为
        if (!readOK)
            _sectionConnectorData->removeBehavior(_elasticityBehaviorData);
        return readOK;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::adaptW()
    {
        _sectionConnectorData = dynamic_cast<Interface::FITKAbaConnectorSection*>(_dataObj);
        if (!_sectionConnectorData || !_elasticityBehaviorData || !_writer)return false;
        bool writeOK = false;
        //获取类型
        Interface::FITKAbaConnectorBehaviorEnum::Definition definitionType = _elasticityBehaviorData->getDefinition();
        Interface::FITKAbaConnectorBehaviorEnum::Coupling couplingType = _elasticityBehaviorData->getCoupling();
        if (definitionType == Interface::FITKAbaConnectorBehaviorEnum::Definition::Linear)
        {
            if (couplingType == Interface::FITKAbaConnectorBehaviorEnum::Coupling::Uncoupled)
                writeOK = this->writeDefinitionLinearTypeUncoupled();
            else if (couplingType == Interface::FITKAbaConnectorBehaviorEnum::Coupling::Coupled)
                writeOK = this->writeDefinitionLinearTypeCoupled();
        }
        else if (definitionType == Interface::FITKAbaConnectorBehaviorEnum::Definition::Nonlinear)
        {
            if (couplingType == Interface::FITKAbaConnectorBehaviorEnum::Coupling::Uncoupled)
                writeOK = this->writeDefinitionNonlinearTypeUncoupled();
            else if (couplingType == Interface::FITKAbaConnectorBehaviorEnum::Coupling::CoupledOnPosition)
                writeOK = this->writeDefinitionNonlinearTypeCoupledPosition();
            else if (couplingType == Interface::FITKAbaConnectorBehaviorEnum::Coupling::CoupledOnMotion)
                writeOK = this->writeDefinitionNonlinearTypeCoupledMotion();
        }
        else if (definitionType == Interface::FITKAbaConnectorBehaviorEnum::Definition::Rigid)
            writeOK = this->writeDefinitionRigidType();
        return writeOK;
    }

    void FITKAbaqusAdaptorConnectorElasticityBehavior::setWriteBehaviorData(Interface::FITKAbaConnectorBehaviorElasticity* elasticityData)
    {
        _elasticityBehaviorData = elasticityData;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::readDefinitionLinearTypeUncoupled()
    {
        if (!_elasticityBehaviorData || !_reader)return false;
        //设置耦合-不耦合
        _elasticityBehaviorData->setCoupling(Interface::FITKAbaConnectorBehaviorEnum::Coupling::Uncoupled);
        //文件应有的一次数据所具有的行数
        int rowParaNum = (3 + _dependencies) % 8 ? (3 + _dependencies) / 8 + 1 : (3 + _dependencies) / 8;
        //获取数据
        bool ok = false;
        int  num = 0;
        int index = 0;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            //动态设置行数
            _elasticityBehaviorData->setDataRowCount(index + 1);
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.trimmed().split(',');
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                ++num;
                if (num == 2 && str.at(j).trimmed().isEmpty())continue;
                if (num == 3 && str.at(j).trimmed().isEmpty())continue;
                //读取数据
                double v = str.at(j).trimmed().toDouble(&ok);
                if (!ok)return false;
                //对应数据设置
                if (num == 1)
                {
                    if (_component == 1)
                        _elasticityBehaviorData->getRowData(index).setD11(v);
                    else if (_component == 2)
                        _elasticityBehaviorData->getRowData(index).setD22(v);
                    else if (_component == 3)
                        _elasticityBehaviorData->getRowData(index).setD33(v);
                    else if (_component == 4)
                        _elasticityBehaviorData->getRowData(index).setD44(v);
                    else if (_component == 5)
                        _elasticityBehaviorData->getRowData(index).setD55(v);
                    else if (_component == 6)
                        _elasticityBehaviorData->getRowData(index).setD66(v);
                }
            }
            if (num % 8 != 0 || num / 8 == rowParaNum)
            {
                ++index;
                if (num < 1)return false;
                num = 0;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::writeDefinitionLinearTypeUncoupled()
    {
        if (!_writer || !_elasticityBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //获取力矩状态
        std::array<bool, 6> forceMomentState = _elasticityBehaviorData->getForceMomentStates();
        //获取行数
        int count = _elasticityBehaviorData->getDataRowCount();
        for (int i = 0; i < 6; ++i)
        {
            if (!forceMomentState[i])continue;
            *stream << QString("*Connector Elasticity, component=%1").arg(i + 1) << endl;
            for (int j = 0; j < count; ++j)
            {
                Interface::ConnectorBHElasticityData data = _elasticityBehaviorData->getRowData(j);
                QString lineOne;
                if (i == 0)
                    lineOne += _writer->double2String(data.getD11(), 8, 0);
                else if (i == 1)
                    lineOne += _writer->double2String(data.getD22(), 8, 0);
                else if (i == 2)
                    lineOne += _writer->double2String(data.getD33(), 8, 0);
                else if (i == 3)
                    lineOne += _writer->double2String(data.getD44(), 8, 0);
                else if (i == 4)
                    lineOne += _writer->double2String(data.getD55(), 8, 0);
                else if (i == 5)
                    lineOne += _writer->double2String(data.getD66(), 8, 0);
                *stream << lineOne + "," << endl;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::readDefinitionLinearTypeCoupled()
    {
        if (!_elasticityBehaviorData)return false;
        //设置耦合-耦合
        _elasticityBehaviorData->setCoupling(Interface::FITKAbaConnectorBehaviorEnum::Coupling::Coupled);
        //文件应有的一次数据所具有的行数
        int rowParaNum = (21 + 1 + _dependencies) % 8 ? (21 + 1 + _dependencies) / 8 + 1 : (21 + 1 + _dependencies) / 8;

        //获取数据
        bool ok = false;
        int  num = 0;
        int index = 0;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            if (line.startsWith("*"))break;
            //动态设置行数
            _elasticityBehaviorData->setDataRowCount(index + 1);
            QStringList str;
            //如果为空行就证明8个数据都为0.0
            if (line.trimmed().isEmpty())
                num += 8;
            else
                str = line.trimmed().split(',');
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                ++num;
                if (str.at(j).trimmed().isEmpty())continue;
                //读取数据
                double v = str.at(j).trimmed().toDouble(&ok);
                if (!ok)return false;
                //对应数据设置
                if (num == 1)
                    _elasticityBehaviorData->getRowData(index).setD11(v);
                else if (num == 2)
                    _elasticityBehaviorData->getRowData(index).setD12(v);
                else if (num == 3)
                    _elasticityBehaviorData->getRowData(index).setD22(v);
                else if (num == 4)
                    _elasticityBehaviorData->getRowData(index).setD13(v);
                else if (num == 5)
                    _elasticityBehaviorData->getRowData(index).setD23(v);
                else if (num == 6)
                    _elasticityBehaviorData->getRowData(index).setD33(v);
                else if (num == 7)
                    _elasticityBehaviorData->getRowData(index).setD14(v);
                else if (num == 8)
                    _elasticityBehaviorData->getRowData(index).setD24(v);
                else if (num == 9)
                    _elasticityBehaviorData->getRowData(index).setD34(v);
                else if (num == 10)
                    _elasticityBehaviorData->getRowData(index).setD44(v);
                else if (num == 11)
                    _elasticityBehaviorData->getRowData(index).setD15(v);
                else if (num == 12)
                    _elasticityBehaviorData->getRowData(index).setD25(v);
                else if (num == 13)
                    _elasticityBehaviorData->getRowData(index).setD35(v);
                else if (num == 14)
                    _elasticityBehaviorData->getRowData(index).setD45(v);
                else if (num == 15)
                    _elasticityBehaviorData->getRowData(index).setD55(v);
                else if (num == 16)
                    _elasticityBehaviorData->getRowData(index).setD16(v);
                else if (num == 17)
                    _elasticityBehaviorData->getRowData(index).setD26(v);
                else if (num == 18)
                    _elasticityBehaviorData->getRowData(index).setD36(v);
                else if (num == 19)
                    _elasticityBehaviorData->getRowData(index).setD46(v);
                else if (num == 20)
                    _elasticityBehaviorData->getRowData(index).setD56(v);
                else if (num == 21)
                    _elasticityBehaviorData->getRowData(index).setD66(v);
            }
            num = num % 8 == 0 ? num : num + 8 - (num % 8);
            if (num / 8 == rowParaNum)
            {
                ++index;
                num = 0;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::writeDefinitionLinearTypeCoupled()
    {
        if (!_writer || !_elasticityBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //写出Connector Elasticity
        *stream << QString("*Connector Elasticity") << endl;
        //获取力矩状态
        std::array<bool, 6> forceMomentState = _elasticityBehaviorData->getForceMomentStates();
        int maxIndex = -1;
        for (int i = 5; i >= 0; --i)
        {
            if (forceMomentState[i] == true)
            {
                maxIndex = i;
                break;
            }
        }
        if (maxIndex == -1)return false;
        //获取行数
        int count = _elasticityBehaviorData->getDataRowCount();
        for (int index = 0; index < count; ++index)
        {
            QList<double> valueList;
            if (maxIndex >= 0)
                valueList.append(_elasticityBehaviorData->getRowData(index).getD11());
            if (maxIndex >= 1)
            {
                valueList.append(_elasticityBehaviorData->getRowData(index).getD12());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD22());
            }
            if (maxIndex >= 2)
            {
                valueList.append(_elasticityBehaviorData->getRowData(index).getD13());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD23());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD33());
            }
            if (maxIndex >= 3)
            {
                valueList.append(_elasticityBehaviorData->getRowData(index).getD14());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD24());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD34());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD44());
            }
            if (maxIndex >= 4)
            {
                valueList.append(_elasticityBehaviorData->getRowData(index).getD15());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD25());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD35());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD45());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD55());
            }
            if (maxIndex >= 5)
            {
                valueList.append(_elasticityBehaviorData->getRowData(index).getD16());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD26());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD36());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD46());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD56());
                valueList.append(_elasticityBehaviorData->getRowData(index).getD66());
            }
            //写出一组数据
            int number = 0;
            QStringList strList;
            for (int i = 0; i < 6; ++i)
            {
                for (int j = 0; j < i + 1; ++j)
                {
                    ++number;
                    QString str = " ";
                    if (forceMomentState[j] && forceMomentState[i])
                    {
                        double v = valueList.at(number - 1);
                        str = _writer->double2String(v, 8, 0);
                    }
                    if (i <= maxIndex)
                        strList.append(str);
                    if (number % 8 == 0 || (i == 5 && j == 5))
                    {
                        *stream << strList.join(",") << endl;
                        strList.clear();
                    }
                }
            }
        }
        
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::readDefinitionNonlinearTypeUncoupled()
    {
        if (!_elasticityBehaviorData)return false;
        //设置耦合-不耦合
        _elasticityBehaviorData->setCoupling(Interface::FITKAbaConnectorBehaviorEnum::Coupling::Uncoupled);
        //文件应有的一次数据所具有的行数
        int rowParaNum = (3 + _dependencies) % 8 ? (3 + _dependencies) / 8 + 1 : (3 + _dependencies) / 8;
        //获取数据
        bool ok = false;
        int  num = 0;
        int index = 0;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            //动态设置行数
            _elasticityBehaviorData->setDataRowCount(index + 1);
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.trimmed().split(',');
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                ++num;
                if (num == 3 && str.at(j).trimmed().isEmpty())continue;
                //读取数据
                double v = str.at(j).trimmed().toDouble(&ok);
                if (!ok)return false;
                //对应数据设置
                if (num == 1)
                    _elasticityBehaviorData->getRowData(index).setForM(v);
                else if (num == 2)
                    _elasticityBehaviorData->getRowData(index).setUorUR(v);
            }
            if (num % 8 != 0 || num / 8 == rowParaNum)
            {
                ++index;
                if (num < 2)return false;
                num = 0;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::writeDefinitionNonlinearTypeUncoupled()
    {
        if (!_writer || !_elasticityBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //获取力矩状态
        std::array<bool, 6> forceMomentState = _elasticityBehaviorData->getForceMomentStates();
        //获取行数
        int count = _elasticityBehaviorData->getDataRowCount();
        for (int i = 0; i < 6; ++i)
        {
            if (!forceMomentState[i])continue;
            *stream << QString("*Connector Elasticity, nonlinear, component=%1").arg(i + 1) << endl;
            for (int j = 0; j < count; ++j)
            {
                Interface::ConnectorBHElasticityData data = _elasticityBehaviorData->getRowData(j);
                QString lineOne = QString("%1,%2").arg(_writer->double2String(data.getForM(), 8, 0)).arg(_writer->double2String(data.getUorUR(), 8, 0));
                *stream << lineOne << endl;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::readDefinitionNonlinearTypeCoupledPosition()
    {
        if (!_elasticityBehaviorData)return false;
        //设置耦合-位置耦合
        _elasticityBehaviorData->setCoupling(Interface::FITKAbaConnectorBehaviorEnum::Coupling::CoupledOnPosition);
        bool ok = false;
        //读取Independent components
        QString line = _reader->readLine().trimmed();
        QStringList str = line.split(',');
        std::array<bool, 6> independentCompoments{ false, false, false, false, false, false };
        QList<int> independentList;
        for (int i = 0; i < str.size(); ++i)
        {
            int num = str.at(i).trimmed().toInt(&ok);
            if (!ok)continue;
            if (num < 1 || num > 6)return false;
            independentCompoments[num - 1] = true;
            independentList.append(num);
        }
        _elasticityBehaviorData->setIndependentComponents(independentCompoments);
        if (independentList.isEmpty())return false;
        //文件应有的一次数据所具有的行数
        int requiredParaNum = 1 + independentList.size();
        int rowParaNum = (1 + requiredParaNum + _dependencies) % 8 ? (1 + requiredParaNum + _dependencies) / 8 + 1 : (1 + requiredParaNum + _dependencies) / 8;
        //获取数据
        int  num = 0;
        int index = 0;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            //动态设置行数
            _elasticityBehaviorData->setDataRowCount(index + 1);
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.trimmed().split(',');
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                ++num;
                if (num == 1 + requiredParaNum && str.at(j).trimmed().isEmpty())continue;
                //读取数据
                double v = str.at(j).trimmed().toDouble(&ok);
                if (!ok)return false;
                //对应数据设置
                if (num == 1)
                {
                    if (_component == 1)
                        _elasticityBehaviorData->getRowData(index).setF1(v);
                    else if (_component == 2)
                        _elasticityBehaviorData->getRowData(index).setF2(v);
                    else if (_component == 3)
                        _elasticityBehaviorData->getRowData(index).setF3(v);
                    else if (_component == 4)
                        _elasticityBehaviorData->getRowData(index).setM1(v);
                    else if (_component == 5)
                        _elasticityBehaviorData->getRowData(index).setM2(v);
                    else if (_component == 6)
                        _elasticityBehaviorData->getRowData(index).setM3(v);
                }
                else if (num <= requiredParaNum)
                {
                    int independent = independentList.at(num - 2);
                    if (independent == 1)
                        _elasticityBehaviorData->getRowData(index).setCP1(v);
                    else if (independent == 2)
                        _elasticityBehaviorData->getRowData(index).setCP2(v);
                    else if (independent == 3)
                        _elasticityBehaviorData->getRowData(index).setCP3(v);
                    else if (independent == 4)
                        _elasticityBehaviorData->getRowData(index).setCPR1(v);
                    else if (independent == 5)
                        _elasticityBehaviorData->getRowData(index).setCPR2(v);
                    else if (independent == 6)
                        _elasticityBehaviorData->getRowData(index).setCPR3(v);
                }
            }
            if (num % 8 != 0 || num / 8 == rowParaNum)
            {
                ++index;
                if (num < requiredParaNum)return false;
                num = 0;
            }
        }

        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::writeDefinitionNonlinearTypeCoupledPosition()
    {
        if (!_writer || !_elasticityBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //获取力矩状态
        std::array<bool, 6> forceMomentState = _elasticityBehaviorData->getForceMomentStates();
        //获取Independent components
        QStringList independentList;
        std::array<bool, 6> independentCompoments = _elasticityBehaviorData->getIndepenentComponents();
        for (int i = 0; i < 6; ++i)
        {
            if (!independentCompoments[i])continue;
            independentList.append(QString::number(i + 1));
        }
        if (independentList.isEmpty())return false;
        //获取行数
        int count = _elasticityBehaviorData->getDataRowCount();
        for (int i = 0; i < 6; ++i)
        {
            if (!forceMomentState[i])continue;
            *stream << QString("*Connector Elasticity, nonlinear, component=%1, independent components=POSITION").arg(i + 1) << endl;
            *stream << independentList.join(", ") << endl;
            for (int j = 0; j < count; ++j)
            {
                Interface::ConnectorBHElasticityData data = _elasticityBehaviorData->getRowData(j);
                QString lineOne;
                if (i == 0)
                    lineOne += _writer->double2String(data.getF1(), 8, 0);
                else if (i == 1)
                    lineOne += _writer->double2String(data.getF2(), 8, 0);
                else if (i == 2)
                    lineOne += _writer->double2String(data.getF3(), 8, 0);
                else if (i == 3)
                    lineOne += _writer->double2String(data.getM1(), 8, 0);
                else if (i == 4)
                    lineOne += _writer->double2String(data.getM2(), 8, 0);
                else if (i == 5)
                    lineOne += _writer->double2String(data.getM3(), 8, 0);
                for (int k = 0; k < independentList.size(); ++k)
                {
                    QString str = independentList.at(k);
                    if (str == "1")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCP1(), 8, 0));
                    if (str == "2")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCP2(), 8, 0));
                    if (str == "3")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCP3(), 8, 0));
                    if (str == "4")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCPR1(), 8, 0));
                    if (str == "5")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCPR2(), 8, 0));
                    if (str == "6")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCPR3(), 8, 0));
                }
                *stream << lineOne << endl;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::readDefinitionNonlinearTypeCoupledMotion()
    {
        if (!_elasticityBehaviorData)return false;
        //设置耦合-运动耦合
        _elasticityBehaviorData->setCoupling(Interface::FITKAbaConnectorBehaviorEnum::Coupling::CoupledOnMotion);
        bool ok = false;
        //读取Independent components
        QString line = _reader->readLine().trimmed();
        QStringList str = line.split(',');
        std::array<bool, 6> independentCompoments{ false, false, false, false, false, false };
        QList<int> independentList;
        for (int i = 0; i < str.size(); ++i)
        {
            int num = str.at(i).trimmed().toInt(&ok);
            if (!ok)continue;
            if (num < 1 || num > 6)return false;
            independentCompoments[num - 1] = true;
            independentList.append(num);
        }
        _elasticityBehaviorData->setIndependentComponents(independentCompoments);
        if (independentList.isEmpty())return false;
        //文件应有的一次数据所具有的行数
        int requiredParaNum = 1 + independentList.size();
        int rowParaNum = (1 + requiredParaNum + _dependencies) % 8 ? (1 + requiredParaNum + _dependencies) / 8 + 1 : (1 + requiredParaNum + _dependencies) / 8;
        //获取数据
        int  num = 0;
        int index = 0;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            //动态设置行数
            _elasticityBehaviorData->setDataRowCount(index + 1);
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.trimmed().split(',');
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                ++num;
                if (num == 1 + requiredParaNum && str.at(j).trimmed().isEmpty())continue;
                //读取数据
                double v = str.at(j).trimmed().toDouble(&ok);
                if (!ok)return false;
                //对应数据设置
                if (num == 1)
                {
                    if (_component == 1)
                        _elasticityBehaviorData->getRowData(index).setF1(v);
                    else if (_component == 2)
                        _elasticityBehaviorData->getRowData(index).setF2(v);
                    else if (_component == 3)
                        _elasticityBehaviorData->getRowData(index).setF3(v);
                    else if (_component == 4)
                        _elasticityBehaviorData->getRowData(index).setM1(v);
                    else if (_component == 5)
                        _elasticityBehaviorData->getRowData(index).setM2(v);
                    else if (_component == 6)
                        _elasticityBehaviorData->getRowData(index).setM3(v);
                }
                else if (num <= requiredParaNum)
                {
                    int independent = independentList.at(num - 2);
                    if (independent == 1)
                        _elasticityBehaviorData->getRowData(index).setCCU1(v);
                    else if (independent == 2)
                        _elasticityBehaviorData->getRowData(index).setCCU2(v);
                    else if (independent == 3)
                        _elasticityBehaviorData->getRowData(index).setCCU3(v);
                    else if (independent == 4)
                        _elasticityBehaviorData->getRowData(index).setCCUR1(v);
                    else if (independent == 5)
                        _elasticityBehaviorData->getRowData(index).setCCUR2(v);
                    else if (independent == 6)
                        _elasticityBehaviorData->getRowData(index).setCCUR3(v);
                }
            }
            if (num % 8 != 0 || num / 8 == rowParaNum)
            {
                ++index;
                if (num < requiredParaNum)return false;
                num = 0;
            }
        }

        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::writeDefinitionNonlinearTypeCoupledMotion()
    {
        if (!_writer || !_elasticityBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //获取力矩状态
        std::array<bool, 6> forceMomentState = _elasticityBehaviorData->getForceMomentStates();
        //获取Independent components
        QStringList independentList;
        std::array<bool, 6> independentCompoments = _elasticityBehaviorData->getIndepenentComponents();
        for (int i = 0; i < 6; ++i)
        {
            if (!independentCompoments[i])continue;
            independentList.append(QString::number(i + 1));
        }
        if (independentList.isEmpty())return false;
        //获取行数
        int count = _elasticityBehaviorData->getDataRowCount();
        for (int i = 0; i < 6; ++i)
        {
            if (!forceMomentState[i])continue;
            *stream << QString("*Connector Elasticity, nonlinear, component=%1, independent components=POSITION").arg(i + 1) << endl;
            *stream << independentList.join(", ") << endl;
            for (int j = 0; j < count; ++j)
            {
                Interface::ConnectorBHElasticityData data = _elasticityBehaviorData->getRowData(j);
                QString lineOne;
                if (i == 0)
                    lineOne += _writer->double2String(data.getF1(), 8, 0);
                else if (i == 1)
                    lineOne += _writer->double2String(data.getF2(), 8, 0);
                else if (i == 2)
                    lineOne += _writer->double2String(data.getF3(), 8, 0);
                else if (i == 3)
                    lineOne += _writer->double2String(data.getM1(), 8, 0);
                else if (i == 4)
                    lineOne += _writer->double2String(data.getM2(), 8, 0);
                else if (i == 5)
                    lineOne += _writer->double2String(data.getM3(), 8, 0);
                for (int k = 0; k < independentList.size(); ++k)
                {
                    QString str = independentList.at(k);
                    if (str == "1")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCCU1(), 8, 0));
                    if (str == "2")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCCU2(), 8, 0));
                    if (str == "3")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCCU3(), 8, 0));
                    if (str == "4")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCCUR1(), 8, 0));
                    if (str == "5")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCCUR2(), 8, 0));
                    if (str == "6")
                        lineOne += QString(",%1").arg(_writer->double2String(data.getCCUR3(), 8, 0));
                }
                *stream << lineOne << endl;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::readDefinitionRigidType()
    {
        if (!_reader || !_elasticityBehaviorData)return false;
        bool ok = false;
        std::array<bool, 6> states{ false, false, false, false, false, false };
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.split(',');
            for (int i = 0; i < str.size(); ++i)
            {
                int v = str.at(i).trimmed().toDouble(&ok);
                if (!ok || v < 1 || v > 6)return false;
                states[v - 1] = true;
            }
        }
        _elasticityBehaviorData->setComponentsStates(states);
        return ok;
    }

    bool FITKAbaqusAdaptorConnectorElasticityBehavior::writeDefinitionRigidType()
    {
        if (!_writer || !_elasticityBehaviorData)return false;
        QTextStream* stream = _writer->getStream();
        //写出Connector Elasticity
        *stream << "*Connector Elasticity, rigid" << endl;
        QStringList str;
        std::array<bool, 6> componentState = _elasticityBehaviorData->getComponentsStates();
        for (int i = 0; i < 6; ++i)
            if (componentState[i])
                str.append(QString::number(i + 1));
        *stream << str.join(", ") << endl;
        return true;
    }
}